/*
 * Author: Christopher Rondot
 * Contact: codiplex@gmail.com
 * Date: August 8, 2009
 * Comment: GTUG Campout Meeting
 */

package com.google.wavesurferrobot.data;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.google.wavesurferrobot.data.objects.BlipData;
import com.google.wavesurferrobot.data.objects.WaveData;
import com.google.wavesurferrobot.data.objects.Tfidf;
import com.google.wavesurferrobot.data.objects.Token;
import com.google.wavesurferrobot.data.util.PMF;

public class FetchWaveData {

	public static List<WaveData> fetchDataWavesFromAuthor(String author) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();

	    List<WaveData> results = null;
	    
		try
		{
		    // Start the transaction
		    tx.begin();
		    Query query = pm.newQuery(WaveData.class);
		    query.setFilter("author == authorParam");
		    //query.setOrdering("hireDate desc");
		    query.declareParameters("String authorParam");
		    
		    results = (List<WaveData>) query.execute(author);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
		return results;
	}
	
	public static WaveData fetchDataWaveFromGoogleWaveID(String googleWaveID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		WaveData result = null;
	    
		try
		{
		    // Start the transaction
		    tx.begin();
		    Query query = pm.newQuery(WaveData.class);
		    query.setFilter("googleWaveID == googleWaveIDParam");
		    //query.setOrdering("hireDate desc");
		    query.declareParameters("String googleWaveIDParam");
		    
	    	List<WaveData> results = (List<WaveData>) query.execute(googleWaveID);
	    	result = results.get(0);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
        return result;
	}
	
	public static List<BlipData> fetchDataBlipsFromGoogleWaveID(String googleWaveID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<BlipData> results = null;
		
		try
		{
		    // Start the transaction
		    tx.begin();
		    Query query = pm.newQuery(WaveData.class);
		    query.setFilter("googleWaveID == googleWaveIDParam");
		    //query.setOrdering("hireDate desc");
		    query.declareParameters("String googleWaveIDParam");
		    results = (List<BlipData>) query.execute(googleWaveID);
		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
        return results;
	}
	
	public static List<Tfidf> fetchTfidfsFromTokenID(Long tokenID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Tfidf> results = null;
		
		try
		{
		    // Start the transaction
		    tx.begin();
		    Query query = pm.newQuery(WaveData.class);
		    query.setFilter("token_id == token_idParam");
		    //query.setOrdering("hireDate desc");
		    query.declareParameters("Long token_idParam");
		    results = (List<Tfidf>) query.execute(tokenID);
		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
        return results;
	}
	
	public static List<Token> fetchTokenClassFromTokenString(String token) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Token> results = null;
		
		try
		{
		    // Start the transaction
		    tx.begin();
		    Query query = pm.newQuery(WaveData.class);
		    query.setFilter("googleWaveID == googleWaveIDParam");
		    //query.setOrdering("hireDate desc");
		    query.declareParameters("String googleWaveIDParam");
		    results = (List<Token>) query.execute(token);
		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
        return results;
	}
	
}
